<template>
	<view class="main">
		<view class="mains">
			<u-sticky>
				<view class="top_c">
					<view class="top">
						<view class="items">
							<view class="lefts">
								<text>代理</text>
							</view>
							<view class="rights">
								<text>{{agentData.name}}</text>
								<text class="phone">{{agentData.phone}}</text>
							</view>
						</view>
						<view class="items">
							<view class="lefts">
								<text>产品</text>
							</view>
							<view class="rights" @click="proShow = true">
								<u-icon size="24" space="8px" :name="product_image"></u-icon>
								<text class="products">{{product_name}}</text>
								<u-icon size="24" space="8px" color="#1296db" name="list"></u-icon>
							</view>
						</view>
					</view>
					<view class="tags">
						<u-subsection mode="subsection" :list="tagList" :current="current"
							@change="sectionChange"></u-subsection>
					</view>
				</view>
			</u-sticky>
			<view class="content">
				<view v-if="toTradeForm != null && toTradeForm.length > 0">
					<view v-if="current == 0" class="types">
						<view class="lists" v-for="(item,index) in toTradeForm" :key="index">
							<view class="title">
								<text>结算费率</text>
							</view>
							<view class="dayFrame" style="display: none;">
								<view class="lefts">
									<input type="text" placeholder="开始天数" disabled="true" v-model="item.start_day">
								</view>
								<view class="rights">
									<input type="text" placeholder="结束天数" disabled="true" v-model="item.end_day">
								</view>
							</view>
							<view class="items" v-for="(items,indexs) in item.details" :key="indexs">
								<view class="lefts">
									<!-- 贷记卡 -->
									<u-icon v-if="items.code === '1' || items.code === '06'"
										:name="'/static/icons/rate_1.png'" size="20"></u-icon>
									<!-- 银联二维码 -->
									<u-icon v-else-if="items.code === '2'" :name="'/static/icons/rate_2.png'"
										size="20"></u-icon>
									<!-- 微信 -->
									<u-icon v-else-if="items.code === '3' || items.code === '02'"
										:name="'/static/icons/rate_3.png'" size="20"></u-icon>
									<!-- 支付宝 -->
									<u-icon
										v-else-if="items.code === '4' || (items.code === '01' && items.product_id == 2)"
										:name="'/static/icons/rate_4.png'" size="20"></u-icon>
									<!-- 扫码 -->
									<u-icon
										v-else-if="items.code === '01' && items.product_id != 2"
										:name="'/static/icons/rate_5.png'" size="20"></u-icon>
									<!-- 云闪付 -->
									<u-icon v-else-if="items.code === '5' || items.code === '07'"
										:name="'/static/icons/rate_6.png'" size="20"></u-icon>
									<text>{{items.name}}</text>
								</view>
								<view class="rights">
									<input type="text" :placeholder="items.min_rates + ' ~ ' + items.max_rates"
										v-model="items.rates">
									<text>%</text>
								</view>
							</view>
						</view>
						<view v-if="myStrokeCount > 0" class="lists">
							<view class="title">
								<text>D0额外定额手续费</text>
							</view>
							<view class="items">
								<view class="lefts">
									<text>结算底价</text>
								</view>
								<view class="rights">
									<input type="text" v-model="toStrokeForm[0].money"
										:placeholder="myStrokeForm[0].money + ' ~ 3.00'">
									<text>元</text>
								</view>
							</view>
						</view>
					</view>
					<view v-else-if="current == 1" class="types">
						<view class="lists" v-for="(item,index) in toActiveForm" :key="index">
							<view class="title">
								<text>激活返现</text>
							</view>
							<view class="dayFrame">
								<view class="lefts">
									<input type="text" placeholder="开始天数" disabled="true" v-model="item.start_day">
								</view>
								<view class="rights">
									<input type="text" placeholder="结束天数" disabled="true" v-model="item.end_day">
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									交易
								</view>
								<view class="rights">
									<input type="text" placeholder="输入交易金额" disabled="true" v-model="item.money">
									<text>元</text>
								</view>
							</view>
							<view class="items">
								<view class="lefts">
									返现
								</view>
								<view class="rights">
									<input type="text" placeholder="输入返现金额" v-model="item.price">
									<text>元</text>
								</view>
							</view>
						</view>
					</view>
					<view v-else-if="current == 2" class="types">
						<view class="lists" v-for="(item,index) in toFlowForm" :key="index">
							<view class="title">
								<view class="lefts">
									<text>流量卡第</text>
									<text>{{item.numbers}}</text>
									<text>次</text>
								</view>
								<view class="rights">
									<text class="setZore" @click="setZore(item)">归 - 0</text>
									<u-icon @click="delAllClick(index)" name="trash" color="#fa3534" size="32"></u-icon>
								</view>
							</view>
							<view class="items" v-for="(items,indexs) in item.details" :key="indexs">
								<view class="lefts">
									流量费 - {{items.flow_money}}
								</view>
								<view class="rights">
									<input type="number" placeholder="输入返现流量费" v-model="items.cash_money">
									<text class="flow_unit">元</text>
									<u-icon @click="delClick(indexs, item.details)" name="trash" color="#fa3534"
										size="32"></u-icon>
								</view>
							</view>
							<view v-if="item.details.length < item.counts" class="items-add">
								<view class="add-btn" @click="addFlowFee(item.details, item, index)">
									<u-icon name="plus" color="#2979ff" size="18"></u-icon>
									<text>增加流量费</text>
								</view>
							</view>
						</view>
						<view v-if="toFlowForm.length < myFlowForm.length" class="lists-add">
							<view class="add-btn" @click="addFlowCard()">
								<u-icon name="plus" color="#ffffff" size="18"></u-icon>
								<text>增加流量卡</text>
							</view>
						</view>
					</view>
				</view>
				<u-empty v-else text="未设置" icon="/static/Empty/data_null.png"></u-empty>
			</view>
			<view class="saveBtn" v-if="settings == 1" @click="saveData">
				<text>{{saveBtnText}}</text>
			</view>
			<view class="saveBtn" v-else-if="settlement_state == 1" @click="saveData">
				<text>{{saveBtnText}}</text>
			</view>
		</view>
		<u-picker :show="proShow" :columns="proColumns" keyName="label" @confirm="proConfirm"
			@cancel="proShow = false"></u-picker>
		<u-picker :show="flowFeeShow" :columns="[flowFeeArr]" keyName="label" @confirm="flowFeeConfirm"
			@cancel="flowFeeShow = false"></u-picker>
		<u-picker :show="flowCardShow" :columns="[flowCardArr]" keyName="label" @confirm="flowCardConfirm"
			@cancel="flowCardShow = false"></u-picker>
	</view>
</template>

<script>
	import tabbarItem from '../../../uni_modules/uview-ui/libs/config/props/tabbarItem'
	var util = require('@/util/request.js');

	export default {
		data() {
			return {
				API_URL: '',
				tagList: ['结算费率', '激活返现', '流量卡'],
				current: 0,
				proShow: false,
				proColumns: [],
				product_id: '',
				product_name: '',
				product_image: '',
				agent_id: 0,
				agentData: {},
				myTradeForm: null,
				myActiveForm: null,
				myFlowForm: null,
				myStrokeForm: null,
				myStrokeCount: 0,
				toTradeForm: null,
				toActiveForm: null,
				toFlowForm: null,
				toStrokeForm: null,
				toStrokeCount: 0,
				settings: 1,
				is_true: true,
				title: '',
				data: [],
				saveBtnText: '保存',
				flowFeeShow: false,
				flowFeeArr: [],
				flowFeeIndex: 0,
				flowCardShow: false,
				flowCardArr: [],
				settlement_state: 1,
				policyMsg: '确定要保存吗？',
				iconList: [{
					icon: 'coupon',
					color: ''
				}, {
					icon: 'coupon',
					color: ''
				}, {
					icon: 'coupon',
					color: ''
				}, {
					icon: 'coupon',
					color: ''
				}]
			}
		},
		onLoad(e) {
			this.API_URL = util.API_URL
			this.agent_id = e.id
			this.getProduct()
			this.getAgent()
			this.getSettlementState()
		},
		methods: {
			sectionChange(index) {
				this.current = index
				// if (index == 0) {
				// 	this.saveBtnText = '下一步'
				// } else {
				// 	this.saveBtnText = '保存'
				// }
			},
			proConfirm(e) {
				this.product_id = e.value[0].id
				this.product_name = e.value[0].label
				this.product_image = this.API_URL + e.value[0].images
				this.proShow = false
				uni.showLoading({
					title: '加载中'
				});
				this.myTradeForm = null
				this.myActiveForm = null
				this.myFlowForm = null
				this.myStrokeForm = null
				this.myStrokeCount = 0
				this.toTradeForm = null
				this.toActiveForm = null
				this.toFlowForm = null
				this.toStrokeForm = null
				this.toStrokeCount = 0
				this.loadData()
			},
			async getProduct() {
				uni.showLoading({
					title: '加载中'
				});
				const {
					data: res
				} = await this.$myHttp({
					url: 'datas/getMyProduct',
					data: {
						type: 2
					}
				});
				if (res.code == 200) {
					this.proColumns = [res.data]
					this.product_id = res.data[0].id
					this.product_name = res.data[0].label
					this.product_image = this.API_URL + res.data[0].images
					this.loadData()
				}
			},
			async getAgent() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'user/getAgent',
					data: {
						agent_id: this.agent_id
					}
				});
				if (res.code == 200) {
					this.agentData = res.data
				}
			},
			async loadData() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'setting/getAgentPolicy',
					data: {
						product_id: this.product_id,
						agent_id: this.agent_id
					}
				});
				uni.hideLoading()
				if (res.code == 200) {
					this.data = res.data
					if (res.data.myPolicy && res.data.myPolicy.trades && res.data.myPolicy != null) {
						this.myTradeForm = res.data.myPolicy.trades
						this.myActiveForm = res.data.myPolicy.actives
						this.myFlowForm = res.data.myPolicy.simcards
						this.myStrokeForm = res.data.myPolicy.stroke
						this.myStrokeCount = res.data.myPolicy.strokeNum
					}
					if (res.data.agentPolicy && res.data.agentPolicy != null && res.data.agentPolicy.trades) {
						this.toTradeForm = res.data.agentPolicy.trades
						this.toActiveForm = res.data.agentPolicy.actives
						this.toFlowForm = res.data.agentPolicy.simcards
						this.toStrokeForm = res.data.agentPolicy.stroke
						this.toStrokeCount = res.data.agentPolicy.strokeNum
					}
					this.settings = res.data.settings
					if (res.data.settings == 2) {
						this.saveBtnText = '修改'
					} else {
						this.saveBtnText = '保存'
					}
					this.current = 0
				}
			},
			saveData() {
				var _this = this
				// if (this.saveBtnText == '下一步') {
				// 	if (this.current == 1) {
				// 		this.saveBtnText = '保存'
				// 	}
				// 	this.current += 1
				// } else {
				for (var i in this.toTradeForm) {
					var tradeItem = this.toTradeForm[i].details
					for (var j in tradeItem) {
						if (tradeItem[j].rates * 1000 > tradeItem[j].max_rates * 1000 || tradeItem[j].rates * 1000 <
							tradeItem[j].min_rates * 1000) {
							uni.showToast({
								title: tradeItem[j].name + '费率设置超出限制！',
								icon: 'none',
								duration: 1000
							})
							return false
						}
					}
				}
				if (this.myStrokeCount > 0) {
					for (var i in this.toStrokeForm) {
						if (this.toStrokeForm[i].money * 100 > 300 || this.toStrokeForm[i].money * 100 < this.myStrokeForm[
								i].money * 100) {
							uni.showToast({
								title: 'D0结算底价设置超出限制！',
								icon: 'none',
								duration: 1000
							})
							return false
						}
					}
				}
				for (var i in this.toActiveForm) {
					if (this.toActiveForm[i].price * 100 > this.myActiveForm[i].price * 100 || this.toActiveForm[i].price *
						100 < 0) {
						uni.showToast({
							title: '激活返现金额设置超出限制！',
							icon: 'none',
							duration: 1000
						})
						return false
					}
				}
				for (var i in this.toFlowForm) {
					var flowItem = this.toFlowForm[i].details
					for (var j in flowItem) {
						if (flowItem[j].cash_money * 100 > flowItem[j].flow_money * 100 || flowItem[j].cash_money * 100 <
							0) {
							uni.showToast({
								title: '流量卡设置超出限制！',
								icon: 'none',
								duration: 1000
							})
							return false
						}
					}
				}
				uni.showModal({
					content: this.policyMsg,
					success: (res) => {
						if (res.confirm) {
							_this.saveDataFun()
						}
					}
				})
				// }
			},
			async saveDataFun() {
				if (this.is_true) {
					this.is_true = false
					uni.showLoading({
						title: '加载中'
					});
					const {
						data: res
					} = await this.$myHttp({
						url: 'setting/saveAgentPolicy',
						data: {
							trades: this.toTradeForm,
							actives: this.toActiveForm,
							strokes: this.toStrokeForm,
							simcards: this.toFlowForm,
							settings: this.settings,
							product_id: this.product_id,
							agent_id: this.agent_id,
							id: this.data.id
						}
					});
					uni.hideLoading()
					this.is_true = true
					uni.showToast({
						title: res.msg,
						icon: 'none',
						duration: 1000
					})
					if (res.code == 200) {
						setTimeout(function() {
							uni.navigateBack({});
						}, 1000)
					}
				}
			},
			setZore(item) {
				for (var i in item.details) {
					item.details[i].cash_money = 0
				}
			},
			delClick(index, data) {
				uni.showModal({
					content: '确定要删除吗',
					success: (res) => {
						if (res.confirm) {
							data.splice(index, 1)
						}
					}
				})
			},
			delAllClick(index) {
				var _this = this
				uni.showModal({
					content: '确定要删除吗',
					success: (res) => {
						if (res.confirm) {
							_this.toFlowForm.splice(index, 1)
						}
					}
				})
			},
			addFlowFee(data, datas, index) {
				this.flowFeeArr = []
				this.flowFeeIndex = index
				for (var i in this.myFlowForm) {
					if (this.myFlowForm[i].numbers == datas.numbers) {
						var myFlowFormDetails = this.myFlowForm[i].details
						for (var j in myFlowFormDetails) {
							var is_exist = true
							for (var k in data) {
								if (myFlowFormDetails[j].flow_money == data[k].flow_money) {
									is_exist = false
								}
							}
							if (is_exist) {
								myFlowFormDetails[j].label = '流量费 - ' + myFlowFormDetails[j].flow_money
								this.flowFeeArr.push(myFlowFormDetails[j])
							}
						}
					}
				}
				this.flowFeeShow = true
			},
			flowFeeConfirm(data) {
				var datas = data.value[0]
				delete datas.id
				delete datas.label
				this.toFlowForm[this.flowFeeIndex].details.push(datas)
				this.flowFeeShow = false
			},
			addFlowCard() {
				this.flowCardArr = []
				for (var i in this.myFlowForm) {
					var is_exist = true
					for (var j in this.toFlowForm) {
						if (this.myFlowForm[i].numbers == this.toFlowForm[j].numbers) {
							is_exist = false
						}
					}
					if (is_exist) {
						this.myFlowForm[i].label = '流量卡第 ' + this.myFlowForm[i].numbers + ' 次'
						this.flowCardArr.push(this.myFlowForm[i])
					}
				}
				this.flowCardShow = true
			},
			flowCardConfirm(data) {
				var datas = data.value[0]
				delete datas.id
				delete datas.label
				for (var i in datas.details) {
					delete datas.details[i].id
				}
				this.toFlowForm.push(datas)
				this.flowCardShow = false
			},
			async getSettlementState() {
				const {
					data: res
				} = await this.$myHttp({
					url: 'v1/getAgentSettlementState'
				});
				if (res.code == 200) {
					this.settlement_state = res.data
					if (res.data == 2 && this.settings == 1) {
						this.policyMsg = '确定要保存吗，一经设置将无法修改！'
					}
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background-color: #F2F6FC;
	}

	input {
		font-size: 28rpx;
		padding: 10rpx;
		border-radius: 10rpx;
		width: 200rpx;
		background-color: #DCDFE6;
	}

	.main {
		.mains {

			.top_c {
				padding: 30rpx;
				background-color: #F2F6FC;

				.top {
					padding: 0 30rpx;
					background-color: #ffffff;
					border-radius: 18rpx;
					box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

					.items {
						padding: 30rpx 0;
						display: flex;
						justify-content: space-between;
						border-bottom: 1px solid #EBEEF5;

						.lefts {}

						.rights {
							display: flex;
							align-items: center;

							.phone {
								padding-left: 16rpx;
							}

							.products {
								padding-right: 16rpx;
								padding-left: 16rpx;
							}
						}
					}
				}

				.tags {
					margin-top: 30rpx;
				}
			}

			.content {
				padding: 0 30rpx;
				margin-bottom: 120rpx;

				.types {

					.lists {
						padding: 30rpx 30rpx 0 30rpx;
						background-color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
						margin-bottom: 30rpx;

						.title {
							text-align: center;
							padding-bottom: 30rpx;
							border-bottom: 1px solid #EBEEF5;
							font-weight: bold;
							display: flex;
							justify-content: space-between;
							align-items: center;

							.rights {
								display: flex;
								align-items: center;

								.setZore {
									padding: 4rpx 18rpx;
									font-size: 24rpx;
									background-color: #fa3534;
									border-radius: 8rpx;
									color: #ffffff;
									font-weight: normal;
									margin-right: 16rpx;
								}
							}
						}

						.dayFrame {
							padding: 20rpx 0;
							display: flex;
							justify-content: space-between;
							border-bottom: 1px solid #EBEEF5;

							.lefts {}

							.rights {
								display: flex;
								justify-content: flex-end;

								input {
									text-align: right;
								}
							}
						}

						.items {
							display: flex;
							justify-content: space-between;
							align-items: center;
							padding: 20rpx 0;
							border-top: 1px solid #EBEEF5;

							.lefts {
								display: flex;
								align-items: center;

								text {
									padding-left: 10rpx;
								}
							}

							.rights {
								display: flex;
								justify-content: flex-end;
								align-items: center;

								input {
									padding-right: 60rpx;
								}

								text {
									position: absolute;
									padding-right: 10rpx;
								}

								.flow_unit {
									padding-right: 80rpx;
								}
							}
						}

						.items-add {
							border-top: 1px solid #EBEEF5;
							padding: 20rpx 0rpx;
							color: #2979ff;

							.add-btn {
								display: flex;
								justify-content: center;
								align-items: center;
								text-align: center;

								text {
									padding-left: 10rpx;
								}
							}
						}
					}

					.lists-add {
						padding: 30rpx 0rpx;
						background-color: #2979ff;
						color: #ffffff;
						border-radius: 18rpx;
						box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);

						.add-btn {
							display: flex;
							justify-content: center;
							align-items: center;
							text-align: center;

							text {
								padding-left: 10rpx;
							}
						}
					}
				}
			}

			.saveBtn {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				text-align: center;
				background-color: #409EFF;
				color: #ffffff;
				padding: 20rpx 0;
			}
		}
	}
</style>